// import Vue from 'vue';

const app = {
  /**/
  state: {
    name: '',
    // 缓存的页面数组
    cachePage: [],
    // 是否全屏
    isFullScreen: false,
    // 要展开的菜单数组
    openedSubmenuArr: [],
    // 主题
    menuTheme: 'dark',
    // 主题颜色
    themeColor: '',
    // 已经打开的页面数组
    pageOpenedList: [{
      title: '首页',
      path: '',
      name: 'home',
      iframeUrl: '',
      isDynamic: false,
      isTab: true
    }],
    // 当前页面名称
    currentPageName: '',
    // 面包屑
    currentPath: [
      {
        title: '首页',
        path: '',
        name: 'home'
      }
    ],
    // 菜单
    menuList: [],
    // 未处理消息总数
    messageCount: 0,
    // iframe文档可视高度(随窗口改变大小)
    documentClientHeight: 0,
    // 不需要缓存的页面列表
    dontCache: []
  },
  mutations: {
    updateName (state, name) {
      state.name = name;
    },
    updateDocumentClientHeight (state, height) {
      state.documentClientHeight = height
    },
    updateMenuList (state, list) {
      state.menuList = list;
    },
    setTagsList (state, list) {
      state.tagsList.push(...list);
    },
    updateMenulist (state) {

    },
    changeMenuTheme (state, theme) {
      state.menuTheme = theme;
    },
    changeMainTheme (state, mainTheme) {
      state.themeColor = mainTheme;
    },
    addOpenSubmenu (state, name) {
      // debugger
      let hasThisName = false;
      let isEmpty = false;
      if (name.length === 0) {
        isEmpty = true;
      }
      if (state.openedSubmenuArr.indexOf(name) > -1) {
        hasThisName = true;
      }
      if (!hasThisName && !isEmpty) {
        state.openedSubmenuArr.push(name);
      }
    },
    closePage (state, name) {
      state.cachePage.forEach((item, index) => {
        if (item === name) {
          state.cachePage.splice(index, 1);
        }
      });
    },
    initCachepage (state) {
      if (localStorage.cachePage) {
        state.cachePage = JSON.parse(localStorage.cachePage);
      }
    },
    removeTag (state, name) {
      state.pageOpenedList.map((item, index) => {
        if (item.name === name) {
          state.pageOpenedList.splice(index, 1);
        }
      });
    },
    pageOpenedList (state, get) {
      let openedPage = state.pageOpenedList[get.index];
      if (get.argu) {
        openedPage.argu = get.argu;
      }
      if (get.query) {
        openedPage.query = get.query;
      }
      state.pageOpenedList.splice(get.index, 1, openedPage);
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    clearAllTags (state) {
      state.pageOpenedList.splice(1);
      state.cachePage.length = 0;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    clearOtherTags (state, vm) {
      let currentName = vm.$route.name;
      let currentIndex = 0;
      state.pageOpenedList.forEach((item, index) => {
        if (item.name === currentName) {
          currentIndex = index;
        }
      });
      if (currentIndex === 0) {
        state.pageOpenedList.splice(1);
      } else {
        state.pageOpenedList.splice(currentIndex + 1);
        state.pageOpenedList.splice(1, currentIndex - 1);
      }
      let newCachepage = state.cachePage.filter(item => {
        return item === currentName;
      });
      state.cachePage = newCachepage;
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    },
    setOpenedList (state) {
      // state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];
    },
    setCurrentPath (state, pathArr) {
      state.currentPath = pathArr;
    },
    setCurrentPageName (state, name) {
      state.currentPageName = name;
    },
    setAvator (state, path) {
      localStorage.avatorImgPath = path;
    },
    clearOpenedSubmenu (state) {
      state.openedSubmenuArr.length = 0;
    },
    setMessageCount (state, count) {
      state.messageCount = count;
    },
    increateTag (state, tagObj) {
      /* if (!Util.oneOf(tagObj.name, state.dontCache)) {
          state.cachePage.push(tagObj.name);
          localStorage.cachePage = JSON.stringify(state.cachePage);
      } */
      state.pageOpenedList.push(tagObj);
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
    }
  }
};

export default app;
